package paper.xml;

import java.util.regex.*;
import java.util.Vector;
import java.util.Iterator;
import java.net.URL;
import java.io.InputStream;
import java.io.InputStreamReader;

public class LinkChaser {
    static Pattern completeURL = Pattern.compile("^[a-z]+://.*");
    static Pattern absoluteURL = Pattern.compile("/.*");

    static Pattern href = Pattern.compile("\\b[Hh][Rr][Ee][Ff]=("+
                                          "([^ \n>]*)|" +
                                          "('[^' \n>]*')|" +
                                          "(\"[^\" \n>]*\"))") ;
    
    public static Vector<String> parseHTML(InputStream is, String base) {
        try {
            char[] charBuffer = new char[4096];
            InputStreamReader isr = new InputStreamReader(is);
            StringBuilder sb = new StringBuilder();
            while (true) {
                int i = isr.read(charBuffer,0, 4096);
                if (i < 0 || sb.length() > 512000) {
                    break;
                }
                sb.append(charBuffer, 0, i);
            }
            Vector<String> links = new Vector();
            String absBase;
            int end = base.indexOf("/",7);
            if (end == -1) {
                base += "/";
                absBase = base;
            } else {
                absBase = base.substring(0,end+1);
                end = base.lastIndexOf("/");
                base = base.substring(0,end+1);
            }
            Matcher m = href.matcher(sb);
            while (m.find()) {
                String hr = m.group(1);
                if (hr.startsWith("'") || hr.startsWith("\"")) {
                    hr = hr.substring(1,hr.length()-1);
                }
                System.out.println(m.group(1));
                if (hr != null && !hr.equals("")) {
                    if (completeURL.matcher(hr).matches()) {
                        links.add(hr);
                    } else if (absoluteURL.matcher(hr).matches()) {
                        links.add(absBase + hr);
                    } else {
                        links.add(base + hr);
                    }
                }
            }
            return links;
        } catch (Exception e) {
            System.out.println("Shit... " + e);
        }
        return null;
    }
}
